package com.mall.server.controller.ums;

import com.mall.model.domain.UmsResource;
import com.mall.model.vo.CommonPage;
import com.mall.model.vo.CommonResult;
import com.mall.server.service.ums.UmsResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : xyiaao
 * @createDate : 2022/3/31 14:07
 */
@CrossOrigin
@RestController
@RequestMapping("/resource")
public class UmsResourceController {

    @Autowired
    private UmsResourceService umsResourceService;


    /**
     * 添加后台资源
     *
     * @param umsResource
     * @return
     */
    @PostMapping("/create")
    public CommonResult create(@RequestBody UmsResource umsResource) {

        umsResourceService.create(umsResource);
        return CommonResult.success();
    }


    /**
     * 根据id删除后台资源
     *
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public CommonResult delete(@PathVariable("id") Integer id) {

        umsResourceService.delete(id);
        return CommonResult.success();
    }


    /**
     * 分页模糊查询后台资源
     *
     * @param categoryId
     * @param nameKeyword
     * @param pageNum
     * @param pageSize
     * @param urlKeyword
     * @return
     */
    @GetMapping("/list")
    public CommonResult list(Integer categoryId, String nameKeyword, Integer pageNum, Integer pageSize, String urlKeyword) {

        CommonPage<UmsResource> commonPage = umsResourceService.list(categoryId, nameKeyword, pageNum, pageSize, urlKeyword);
        return CommonResult.success(commonPage);
    }


    /**
     * 查询所有后台资源
     *
     * @return
     */
    @GetMapping("/listAll")
    public CommonResult listAll() {

        List<UmsResource> list = umsResourceService.listAll();
        return CommonResult.success(list);
    }


    /**
     * 修改后台资源
     *
     * @param id
     * @param umsResource
     * @return
     */
    @PostMapping("/update/{id}")
    public CommonResult update(@PathVariable("id") Integer id, @RequestBody UmsResource umsResource) {

        umsResourceService.update(umsResource);
        return CommonResult.success();
    }


    /**
     * 根据id获取资源详情
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public CommonResult getUmsResource(@PathVariable("id") Integer id) {

        UmsResource umsResource = umsResourceService.getUmsResourceById(id);
        return CommonResult.success(umsResource);
    }

}
